package com.example.novel.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.novel.domain.Result;
import com.example.novel.domain.User;
import com.example.novel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Objects;
import java.util.Random;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping("/LoginUser/{phone}/{password}")
    public Result LoginUser(@PathVariable String phone, @PathVariable String password) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                .eq(User::getPhone, phone);// 根据手机号查询
        User luckinUser = userService.getOne(wrapper);// 获取用户
        Result result = new Result();

        if (luckinUser == null) {
            result.setData("NO");
            return result;
        }
        if (Objects.equals(phone, luckinUser.getPhone()) && Objects.equals(password, luckinUser.getPassword())){
            result.setData("OK");
            return result;
        }else {
            result.setData("ERROR");
            return result;
        }
    }

    // 获取用户，用于修改用户信息
    @ResponseBody
    @RequestMapping("/phone/{phone}")
    public Result getphone(@PathVariable String phone) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                .eq(User::getPhone, phone);
        User luckinUser;
        luckinUser = userService.getOne(wrapper);// 获取用户信息
        Result result = new Result();
        result.setData(luckinUser);// 返回用户信息
        return result;
    }
    //注册
    @ResponseBody
    @RequestMapping("/SaveUser")
    public Result SaveUser(@RequestBody User user) {
        user.setId(null);
        userService.save(user);
        Result result = new Result();
        return result;
    }
    //修改用户资料
    @ResponseBody
    @RequestMapping("/UpdateUser")
    public Result UpdateUser(@RequestBody User user) {
        userService.updateById(user);
        Result result = new Result();
        return result;
    }
}
